package first.group.ssh.gui.dialog;

import first.group.ssh.gui.model.ConnectModel;

public class ConnectDialog extends javax.swing.JDialog {
    
    private final ConnectModel model;
    
    public ConnectDialog(java.awt.Frame parent, boolean modal) {
        this(parent, modal, new ConnectModel());
    }

    @SuppressWarnings("LeakingThisInConstructor")
    public ConnectDialog(java.awt.Frame parent, boolean modal, ConnectModel model) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(parent);
        
        this.model = model;
        model.bindToFrame(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        javax.swing.JPanel connectPanel = new javax.swing.JPanel();
        javax.swing.JLabel hostLabel = new javax.swing.JLabel();
        hostField = new javax.swing.JTextField();
        javax.swing.JLabel portLabel = new javax.swing.JLabel();
        portField = new javax.swing.JTextField();
        javax.swing.JPanel actionsPanel = new javax.swing.JPanel();
        connectButton = new javax.swing.JButton();
        javax.swing.JButton cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("连接到服务器");

        connectPanel.setLayout(new java.awt.GridBagLayout());

        hostLabel.setText("主机名：");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(16, 16, 16, 16);
        connectPanel.add(hostLabel, gridBagConstraints);

        hostField.setPreferredSize(new java.awt.Dimension(192, 24));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(16, 0, 16, 16);
        connectPanel.add(hostField, gridBagConstraints);

        portLabel.setText("端口：");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 16, 16, 16);
        connectPanel.add(portLabel, gridBagConstraints);

        portField.setPreferredSize(new java.awt.Dimension(192, 24));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 16, 16);
        connectPanel.add(portField, gridBagConstraints);

        getContentPane().add(connectPanel, java.awt.BorderLayout.CENTER);

        actionsPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.TRAILING));

        connectButton.setText("连接");
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectButtonActionPerformed(evt);
            }
        });
        actionsPanel.add(connectButton);
        getRootPane().setDefaultButton(connectButton);

        cancelButton.setText("取消");
        cancelButton.setDefaultCapable(false);
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        actionsPanel.add(cancelButton);

        getContentPane().add(actionsPanel, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
        model.doConnect();
    }//GEN-LAST:event_connectButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton connectButton;
    public javax.swing.JTextField hostField;
    public javax.swing.JTextField portField;
    // End of variables declaration//GEN-END:variables
}
